package com.mini.cn.goods.dao.impl;

import com.mini.cn.goods.common.PageQueryResult;
import com.mini.cn.goods.dao.BaseDao;
import com.mini.cn.goods.dao.DestDTO;
import com.mini.cn.goods.dao.IOrderDao;
import com.mini.cn.goods.po.OrderInfo;
import com.mini.cn.goods.util.TimeUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by fly on 17/7/18.
 */
@Repository("orderDao")
public class OrderDaoImpl extends BaseDao<OrderInfo> implements IOrderDao {

    public List<OrderInfo> findOrderByOrderId(String orderId, String openid, Integer appid) throws Exception {
        StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo order where 1=1");
        if (orderId != null) {
            builder.append(" and order.cOrderid='" + orderId + "'");
        }
        if (openid != null) {
            builder.append(" and order.cOpenid='" + openid + "'");
        }
        if (appid != null) {
            builder.append(" and order.cAppid=" + appid);
        }
        List<OrderInfo> list = find(builder.toString());
        return list;
    }


    public List<OrderInfo> findFinishOrderByOpneid(String openid, int appid) throws Exception {
        StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo order where 1=1");
        if (openid != null) {
            builder.append(" and order.cOpenid='" + openid + "'");
        }
        builder.append(" and order.cAppid=" + appid);
        builder.append(" and order.cStatus=1");
        builder.append(" order by  order.cCtime desc ");
        List<OrderInfo> list = find(builder.toString());
        return list;
    }


    public PageQueryResult<DestDTO> getOrderInfoPage(String openid, int pageindex, int pagesize, int appid) throws Exception {
        List<Object> paramsList = new ArrayList<Object>();
        StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo order where 1=1");
        if (openid != null) {
            builder.append(" and order.cOpenid=?0");
            paramsList.add(openid);
        }
        builder.append(" and order.cAppid=?1");
        paramsList.add(appid);
        builder.append(" and order.cStatus>0");
        builder.append(" order by  order.cCtime desc ");
        return this.commonQuery(builder.toString(), paramsList.toArray(), pageindex, pagesize, DestDTO.class);
    }

    public List<OrderInfo> findUserOrderByTime(final String openid, final Date btime, final Date etime, final int appid) throws Exception {
        final StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo o where ");
        builder.append(" o.cStatus=1 ");
        builder.append(" and o.cOpenid=?0");
        builder.append(" and date(o.cCtime)>=?1");
        builder.append(" and date(o.cCtime)<=?2");
        builder.append(" and o.cAppid=?3");
        return getHibernateTemplate().execute(new HibernateCallback<List<OrderInfo>>() {

            public List<OrderInfo> doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(builder.toString());
                query.setParameter(0, openid);
                query.setParameter(1, btime);
                query.setParameter(2, etime);
                query.setParameter(3, appid);
                return query.list();
            }
        });
    }

    @Override
    public PageQueryResult<DestDTO> findOrderByTimeAndStatus(int pageindex, int pagesize, int daynumber, int appid) throws Exception {
        List<Object> paramsList = new ArrayList<Object>();
        StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo orderinfo where 1=1");
        builder.append(" and orderinfo.cStatus=2");
        builder.append(" and orderinfo.cAppid=?0");
        paramsList.add(appid);
        builder.append(" and date(orderinfo.cMtime)<=?1");
        Date mday = TimeUtil.getCurrDay(daynumber);
        paramsList.add(mday);
        return this.commonQuery(builder.toString(), paramsList.toArray(), pageindex, pagesize, DestDTO.class);
    }

    @Override
    public PageQueryResult<DestDTO> findGroupOrderByTimeAndStatus(int pageIndex, int pageSize, int dayNumber, int appId) throws Exception {
        List<Object> paramsList = new ArrayList<Object>();
        StringBuilder builder = new StringBuilder();
        builder.append("from OrderInfo orderinfo where 1=1");
        builder.append(" and orderinfo.cStatus=1");
        builder.append(" and orderinfo.cAppid=?0");
        paramsList.add(appId);
        builder.append(" and date(orderinfo.cMtime)=?1");
        Date mday = TimeUtil.getCurrDay(0);
        paramsList.add(mday);
        return this.commonQuery(builder.toString(), paramsList.toArray(), pageIndex, pageSize, DestDTO.class);
    }
}
